﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace SPFeatureGenerator
{
    public partial class MultipleContentTypesForm : Form
    {
        private List<ContentType> _contentTypes;

        public MultipleContentTypesForm()
        {
            InitializeComponent();

            cmbScope.SelectedIndex = 0;
        }

        public List<ContentType> ContentTypes
        {
            get { return _contentTypes; }
            set { _contentTypes = value; }
        }

        private void gridContentTypes_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dgv = (DataGridView)sender;
            ContentType selected = (ContentType)dgv.Rows[e.RowIndex].DataBoundItem;

            ContentTypeForm form = new ContentTypeForm(selected);
            form.ShowDialog();
        }

        private void btnAddContentType_Click(object sender, EventArgs e)
        {
            ContentTypeForm form = new ContentTypeForm(new ContentType(editFeatureName.Text));
            DialogResult result = form.ShowDialog();
            if (result == DialogResult.OK)
            {
                contentTypeBindingSource.Add(form.ContentType);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DialogResult result = featureFolderBrowser.ShowDialog();
            if (result == DialogResult.OK)
            {
                // Feature.xml
                string featureName = editFeatureName.Text;
                string featureXML = String.Format(Utils.getResource("MCTfeature.xml"),
                    Guid.NewGuid().ToString(),
                    featureName,
                    editDescription.Text,
                    cmbScope.SelectedItem);

                string featureFolder = Path.Combine(featureFolderBrowser.SelectedPath, featureName);
                Directory.CreateDirectory(featureFolder);
                
                string featureFile = Path.Combine(featureFolder, "feature.xml");
                File.WriteAllText(featureFile, featureXML);

                // contentTypeFragments
                string contentTypeFragments = string.Empty;
                foreach (ContentType contentType in contentTypeBindingSource)
                {
                    contentTypeFragments += contentType.toXML();
                }

                // Elements.xml
                string elementsXML = String.Format(Utils.getResource("MCTelements.xml"),
                    Guid.NewGuid().ToString(), contentTypeFragments);

                string elementsFile = Path.Combine(featureFolder, "elements.xml");
                File.WriteAllText(elementsFile, elementsXML);
                
                // Copy templates
                foreach (ContentType contentType in contentTypeBindingSource)
                {
                    if (contentType.TemplateName != null)
                    {
                        string destinationFile = Path.Combine(featureFolder, contentType.TemplateName);
                        if (!File.Exists(destinationFile))
                        {
                            File.Copy(contentType.TemplateFullName, destinationFile);
                        }
                    }
                }

                Close();
            }
        }

        private void editText_Validating(object sender, CancelEventArgs e)
        {
            // No valiadtion when [X] is clicked.
            if (ClientRectangle.Contains(PointToClient(Cursor.Position)))
            {
                e.Cancel = !Utils.validateTextBox((TextBox)sender);
            }
        }
    }
}
